package com.example.healkepdoctor.View.diet.fragement;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.healkepdoctor.R;
import com.example.healkepdoctor.View.diet.helper.dietDataHelper;
import com.example.healkepdoctor.model.dietPlan;
import com.example.healkepdoctor.model.doctorInfo;
import com.example.healkepdoctor.model.patientInfo;


public class dietShowFragment extends Fragment {
    private TextView targetName,date,mainFood,vegetables,fruit,meatAndEgg,bean,milk,nut,oil,salt,totalFood,totalEnergy,doctorName;
    private dietPlan plan;
    private View root;
    public dietShowFragment(dietPlan plan){
        this.plan = plan;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(root == null){
            root = inflater.inflate(R.layout.fragment_diet_show, container, false);
            initComponent();
            bindData();
        }
        return root;
    }
    public void initComponent(){
        targetName = root.findViewById(R.id.tv_targetName);
        date = root.findViewById(R.id.tv_date);
        mainFood = root.findViewById(R.id.tv_mainFood);
        vegetables = root.findViewById(R.id.tv_vegetables);
        fruit = root.findViewById(R.id.tv_fruit);
        meatAndEgg = root.findViewById(R.id.tv_meatAndEgg);
        bean = root.findViewById(R.id.tv_bean);
        milk = root.findViewById(R.id.tv_milk);
        nut = root.findViewById(R.id.tv_nut);
        oil = root.findViewById(R.id.tv_oil);
        salt = root.findViewById(R.id.tv_salt);
        totalFood = root.findViewById(R.id.tv_totalFood);
        totalEnergy = root.findViewById(R.id.tv_totalEnergy);
        doctorName = root.findViewById(R.id.tv_doctorName);

    }
    public void bindData(){
        String date = "从"+plan.getStart()+"到"+plan.getEnd();
        this.date.setText(date);
        this.mainFood.setText(plan.getMainFood());
        this.vegetables.setText(plan.getVegetables());
        this.fruit.setText(plan.getFruit());
        this.meatAndEgg.setText(plan.getMeatAndEgg());
        this.bean.setText(plan.getBean());
        this.milk.setText(plan.getMilk());
        this.nut.setText(plan.getNut());
        this.oil.setText(plan.getOil());
        this.salt.setText(plan.getSalt());
        this.totalFood.setText(String.valueOf(plan.getTotalFood()));
        this.totalEnergy.setText(String.valueOf(plan.getTotalEnergy()));
        dietDataHelper helper = new dietDataHelper(new InfoHandler());
        helper.getDoctorInfo(plan.getDoctorUsername());
        helper.getPatientInfo(plan.getTargetUsername());
    }

    class InfoHandler extends Handler{

        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what == dietDataHelper.SUCCESS){
               Bundle bundle =  msg.getData();
               patientInfo pInfo = bundle.getParcelable("patientInfo");
               doctorInfo dInfo = bundle.getParcelable("doctorInfo");
               if(pInfo!=null){
                   targetName.setText(pInfo.getClientName());
               }
               if(dInfo!=null){
                   doctorName.setText(dInfo.getName());
               }
            }else if(msg.what == dietDataHelper.CONNECT_FAILED){
                Toast.makeText(getContext(), "网络异常",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(getContext(), "未知错误",Toast.LENGTH_SHORT).show();
            }
        }
    }
}